package com.yishan.controller;

import com.github.pagehelper.PageInfo;
import com.yishan.domain.Dept;
import com.yishan.domain.Type;
import com.yishan.service.DrawService;
import com.yishan.service.TypeService;
import com.yishan.utils.BeanUtil;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author yishan
 * @Date 2021/4/28 0028 14:47
 * @Version 1.0
 */
@Controller
@RequestMapping("/type")
public class TypeController {

	@Autowired
	private TypeService typeService;

	@Autowired
	private DrawService drawService;

	/**
	 * 查询所有
	 *
	 * @param model
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/list")
	private String list(Model model, HttpServletRequest request)
			throws ServletException, IOException {

		int page = 1;
		int size = 5;

		if (StringUtils.isNotBlank(request.getParameter("page"))) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (StringUtils.isNotBlank(request.getParameter("size"))) {
			size = Integer.parseInt(request.getParameter("size"));
		}

		PageInfo info = typeService.findAll(page, size);

		model.addAttribute("page", info);

		return "/WEB-INF/pages/type/list.jsp";

	}

	/**
	 * 跳转到添加页面
	 *
	 * @return
	 */
	@RequestMapping("/toAdd")
	private String toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		return "/WEB-INF/pages/type/add.jsp";

	}

	/**
	 * 添加操作
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	private String save(HttpServletRequest request) {

		Type type = BeanUtil.fillBean(request, Type.class, "yyyy-MM-dd");
		typeService.save(type);

		return "redirect:" + request.getContextPath() + "/type/list";

	}

	/**
	 * 跳转到修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	private String toEdit(String id, Model model) {

		Type type = typeService.findById(id);

		model.addAttribute("type", type);

		return "/WEB-INF/pages/type/update.jsp";

	}

	/**
	 * 修改操作
	 *
	 * @param type
	 * @return
	 */
	@RequestMapping("/edit")
	private String edit(Type type) {

		typeService.update(type);

		return "redirect:/type/list";

	}

	/**
	 * 删除操作
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	private String delete(String ids) {
		System.out.println(ids);

		typeService.deleteMany(ids);

		return "redirect:/type/list";

	}



}
